package com.smartguard.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smartguard.bean.User;
import com.smartguard.bean.UserInfo;
import com.smartguard.common.Const;
import com.smartguard.service.UserInfoService;
import com.smartguard.util.JSONUtil;

public class UserInfoAction extends BaseAction {
	private UserInfoService userInfoService;
	
	private UserInfo userInfo;
	
	/**是否开启群组监控
	 * 即是否接收事件
	 */
	private boolean monitor;
	
	public UserInfoAction(){
		this.userInfoService = new UserInfoService();
	}
	
	public void updateUserInfo() throws IOException{
		PrintWriter out = this.getWriter();
		User user = (User) this.getSession().get(Const.SESSION_USER);
		if(user == null){
			out.write(JSONUtil.newErrMsgJSONObject("未登录").toJSONString());
		} else {
			if(this.userInfo != null){
				//判断必填项是否已填
				if(this.userInfo.getContactEmail()==null||"".equals(this.userInfo.getContactEmail())||
						this.userInfo.getAddress()==null||"".equals(this.userInfo.getAddress())||
						this.userInfo.getFirstContact()==null||"".equals(this.userInfo.getFirstContact())||
						this.userInfo.getEnmergencyContact()==null||"".equals(this.userInfo.getEnmergencyContact())){
					out.write(JSONUtil.newErrMsgJSONObject("请填写必填项！").toJSONString());
					out.flush();
					out.close();
					return;
				}
				
				Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
				Matcher matcher = pattern.matcher(this.userInfo.getContactEmail());
				if(!matcher.matches()){
					out.write(JSONUtil.newErrMsgJSONObject("邮箱格式不正确").toJSONString());
					out.flush();
					out.close();
					return;
				}
				
				
				this.userInfo.setUsername(user.getUsername());	//注入用户名
				boolean flag = false;
				//判断该用户之前是否已经创建了userinfo
				if(this.userInfoService.findUserInfo(user.getUsername())==null) {
					flag = this.userInfoService.insertUserInfo(this.userInfo);
				} else {
					flag = this.userInfoService.updateUserInfo(user.getUsername(), this.userInfo);
				}
				if(flag) out.write(JSONUtil.newSuccessJSONObject().toJSONString());
				else out.write(JSONUtil.newErrMsgJSONObject("修改用户信息失败").toJSONString());
			} else {
				out.write(JSONUtil.newErrMsgJSONObject("提交用户信息失败").toJSONString());
			}
		}
		
		out.flush();
		out.close();
	}
	
	public void getUserInfoAction() throws IOException{
		PrintWriter out = this.getWriter();
		User user = (User) this.getSession().get(Const.SESSION_USER);
		if(user == null){
			out.write(JSONUtil.newErrMsgJSONObject("未登录").toJSONString());
		} else {
			UserInfo info = this.userInfoService.findUserInfo(user.getUsername());
			if(info != null){
				JSONObject json = JSONUtil.newSuccessJSONObject();
				json.put("userInfo", JSON.toJSON(info));
				System.out.println("get info");
				out.write(json.toJSONString());
			} else {
				out.write(JSONUtil.newErrMsgJSONObject("暂未添加用户信息").toJSONString());
			}
		}
		
		out.flush();
		out.close();
	}

	/**
	 * 开启或者关闭群组监控
	 * @throws IOException
	 */
	public void changeMonitor() throws IOException {
		// 检测是否登录了，
		User user= (User) this.getSession().get(Const.SESSION_USER);
		PrintWriter out = this.getWriter();
		if(user == null){
			JSONObject json = JSONUtil.newErrMsgJSONObject("未登录");
			out.write(json.toJSONString());
		}
		else{
			UserInfo info = this.userInfoService.findUserInfo(user.getUsername());
			if(info != null){
				info.setMonitor(monitor);
				JSONObject json = JSONUtil.newSuccessJSONObject();
				out.write(json.toJSONString());
			} else {
				out.write(JSONUtil.newErrMsgJSONObject("暂未添加用户信息").toJSONString());
			}
		}
		out.flush();
		out.close();
	}
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	
	public boolean getMonitor() {
		return monitor;
	}

	public void setMonitor(boolean monitor) {
		this.monitor = monitor;
	}
}
